package com.read.operationsapi.service;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mc.read.common.constant.Constants;
import com.mc.read.common.emun.ServiceCodeEnum;
import com.mc.read.common.exception.ServiceException;
import com.mc.read.dao.mapper.BookinformationMapper;
import com.mc.read.dao.mapper.StatisticsMapper;
import com.mc.read.dao.mapper.UserinformationMapper;
import com.mc.read.dao.models.Bookinformation;
import com.mc.read.dao.models.UserinformationExample;
import com.mc.read.dao.vo.*;
import com.mc.read.vo.request.BaseRequest;
import com.mc.read.vo.respose.BaseRespose;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author liuhao
 * @date 2019/6/17 18:38
 */
@Service
@Slf4j
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
public class StatisticsService {
    @Autowired
    StatisticsMapper statisticsMapper;
    @Autowired
    UserinformationMapper userinformationMapper;
    @Autowired
    BookinformationMapper bookinformationMapper;


    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 下载量统计 statisticsForDownload
     **/
    public BaseRespose statisticsForDownload(BaseRequest baseRequest) {
        log.info("下载量统计,参数为{}", JSON.toJSONString(baseRequest));

        BaseRespose baseRespose = new BaseRespose();
        try {
            PageVo pageVo = null;
            try {
                pageVo = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), PageVo.class);
            } catch (Exception e) {
                throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL, JSON.toJSONString(baseRequest));
            }
            List<StatisticsVo> statisticsVo = null;
            //按浏览器
            if (pageVo.getStatype() == 2) {
                statisticsVo = statisticsMapper.statisticsForBrowser(pageVo);
            } else {
                //按城市
                statisticsVo = statisticsMapper.downloadStatisticsForCity(pageVo);
            }
            baseRespose.setReturnString(statisticsVo);
        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("下载量统计 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("下载量统计 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 用户注册统计 staUserReg
     **/
    public BaseRespose staUserReg(BaseRequest baseRequest) {
        log.info("用户注册统计,参数为{}", JSON.toJSONString(baseRequest));

        BaseRespose baseRespose = new BaseRespose();
        try {
            PageVo pageVo = null;
            try {
                pageVo = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), PageVo.class);
            } catch (Exception e) {
                throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL, JSON.toJSONString(baseRequest));
            }
            if (ObjectUtils.isEmpty(pageVo.getStartday()) || ObjectUtils.isEmpty(pageVo.getEndday())) {
                throw new ServiceException(203020, "请选择开始和结束时间", JSON.toJSONString(baseRequest));
            }
            Date startday = null;
            Date endday = null;
            try {
                startday = new SimpleDateFormat("yyyy-MM-dd").parse(pageVo.getStartday());
                endday = new SimpleDateFormat("yyyy-MM-dd").parse(pageVo.getEndday());
            } catch (ParseException e) {
                throw new ServiceException(203020, "时间格式错误", JSON.toJSONString(baseRequest));
            }
            if (startday.getTime() > endday.getTime()) {
                throw new ServiceException(203020, "开始时间不能小于结束时间", JSON.toJSONString(baseRequest));
            }
            List<StatisticsVo> statisticsVo = null;
            //按月
            if (pageVo.getStatype() == 2) {
                String startMonth = new SimpleDateFormat("yyyy-MM").format(startday);
                String endMonth = new SimpleDateFormat("yyyy-MM").format(endday);
                statisticsVo = statisticsMapper.staUserRegForMonth(pageVo);
                Map<String, Long> map = new HashMap<>();
                for (StatisticsVo sta : statisticsVo
                        ) {
                    map.put(sta.getTime(), sta.getCount());
                }
                statisticsVo = new ArrayList<>();
                StatisticsVo statisticsVo1 = new StatisticsVo();
                if (ObjectUtils.isEmpty(map.get(startMonth))) {
                    statisticsVo1.setTime(startMonth);
                    statisticsVo1.setCount(0L);
                } else {
                    statisticsVo1.setTime(startMonth);
                    statisticsVo1.setCount(map.get(startMonth));
                }
                statisticsVo.add(statisticsVo1);
                Calendar calendar = Calendar.getInstance();
                while (!startMonth.equals(endMonth)) {
                    statisticsVo1 = new StatisticsVo();
                    calendar.setTime(startday);
                    calendar.add(Calendar.MONTH, 1);
                    startday = calendar.getTime();
                    startMonth = new SimpleDateFormat("yyyy-MM").format(startday);
                    if (ObjectUtils.isEmpty(map.get(startMonth))) {
                        statisticsVo1.setTime(startMonth);
                        statisticsVo1.setCount(0L);
                    } else {
                        statisticsVo1.setTime(startMonth);
                        statisticsVo1.setCount(map.get(startMonth));
                    }
                    statisticsVo.add(statisticsVo1);
                }
                //按日
            } else {
                statisticsVo = statisticsMapper.staUserReg(pageVo);
                Map<String, Long> map = new HashMap<>();
                for (StatisticsVo sta : statisticsVo
                        ) {
                    map.put(sta.getTime(), sta.getCount());
                }
                String startDay = pageVo.getStartday();
                String endDay = pageVo.getEndday();
                statisticsVo = new ArrayList<>();
                StatisticsVo statisticsVo1 = new StatisticsVo();
                if (ObjectUtils.isEmpty(map.get(startDay))) {
                    statisticsVo1.setTime(startDay);
                    statisticsVo1.setCount(0L);
                } else {
                    statisticsVo1.setTime(startDay);
                    statisticsVo1.setCount(map.get(startDay));
                }
                statisticsVo.add(statisticsVo1);
                Calendar calendar = Calendar.getInstance();
                while (!startDay.equals(endDay)) {
                    statisticsVo1 = new StatisticsVo();
                    calendar.setTime(startday);
                    calendar.add(Calendar.DATE, 1);
                    startday = calendar.getTime();
                    startDay = new SimpleDateFormat("yyyy-MM-dd").format(startday);
                    if (ObjectUtils.isEmpty(map.get(startDay))) {
                        statisticsVo1.setTime(startDay);
                        statisticsVo1.setCount(0L);
                    } else {
                        statisticsVo1.setTime(startDay);
                        statisticsVo1.setCount(map.get(startDay));
                    }
                    statisticsVo.add(statisticsVo1);
                }
            }
            baseRespose.setReturnString(statisticsVo);
        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("用户注册统计 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("用户注册统计 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 活跃用户统计 staActiveUser
     **/
    public BaseRespose staActiveUser(BaseRequest baseRequest) {
        log.info("活跃用户统计,参数为{}", JSON.toJSONString(baseRequest));

        BaseRespose baseRespose = new BaseRespose();
        try {
            PageVo pageVo = null;
            try {
                pageVo = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), PageVo.class);
            } catch (Exception e) {
                throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL, JSON.toJSONString(baseRequest));
            }
            if (ObjectUtils.isEmpty(pageVo.getStartday()) || ObjectUtils.isEmpty(pageVo.getEndday())) {
                throw new ServiceException(203020, "请选择开始和结束时间", JSON.toJSONString(baseRequest));
            }
            Date startday = null;
            Date endday = null;
            try {
                startday = new SimpleDateFormat("yyyy-MM-dd").parse(pageVo.getStartday());
                endday = new SimpleDateFormat("yyyy-MM-dd").parse(pageVo.getEndday());
            } catch (ParseException e) {
                throw new ServiceException(203020, "时间格式错误", JSON.toJSONString(baseRequest));
            }
            if (startday.getTime() > endday.getTime()) {
                throw new ServiceException(203020, "开始时间不能小于结束时间", JSON.toJSONString(baseRequest));
            }
            List<StatisticsVo> statisticsVo = null;
            //按月
            if (pageVo.getStatype() == 2) {
                String startMonth = new SimpleDateFormat("yyyy-MM").format(startday);
                String endMonth = new SimpleDateFormat("yyyy-MM").format(endday);
                statisticsVo = statisticsMapper.staActiveUserForMonth(pageVo);
                Map<String, Long> map = new HashMap<>();
                for (StatisticsVo sta : statisticsVo
                        ) {
                    map.put(sta.getTime(), sta.getCount());
                }
                statisticsVo = new ArrayList<>();
                StatisticsVo statisticsVo1 = new StatisticsVo();
                if (ObjectUtils.isEmpty(map.get(startMonth))) {
                    statisticsVo1.setTime(startMonth);
                    statisticsVo1.setCount(0L);
                } else {
                    statisticsVo1.setTime(startMonth);
                    statisticsVo1.setCount(map.get(startMonth));
                }
                statisticsVo.add(statisticsVo1);
                Calendar calendar = Calendar.getInstance();
                while (!startMonth.equals(endMonth)) {
                    statisticsVo1 = new StatisticsVo();
                    calendar.setTime(startday);
                    calendar.add(Calendar.MONTH, 1);
                    startday = calendar.getTime();
                    startMonth = new SimpleDateFormat("yyyy-MM").format(startday);
                    if (ObjectUtils.isEmpty(map.get(startMonth))) {
                        statisticsVo1.setTime(startMonth);
                        statisticsVo1.setCount(0L);
                    } else {
                        statisticsVo1.setTime(startMonth);
                        statisticsVo1.setCount(map.get(startMonth));
                    }
                    statisticsVo.add(statisticsVo1);
                }
                //按日
            } else {
                statisticsVo = statisticsMapper.staActiveUser(pageVo);
                Map<String, Long> map = new HashMap<>();
                for (StatisticsVo sta : statisticsVo
                        ) {
                    map.put(sta.getTime(), sta.getCount());
                }
                String startDay = pageVo.getStartday();
                String endDay = pageVo.getEndday();
                statisticsVo = new ArrayList<>();
                StatisticsVo statisticsVo1 = new StatisticsVo();
                if (ObjectUtils.isEmpty(map.get(startDay))) {
                    statisticsVo1.setTime(startDay);
                    statisticsVo1.setCount(0L);
                } else {
                    statisticsVo1.setTime(startDay);
                    statisticsVo1.setCount(map.get(startDay));
                }
                statisticsVo.add(statisticsVo1);
                Calendar calendar = Calendar.getInstance();
                while (!startDay.equals(endDay)) {
                    statisticsVo1 = new StatisticsVo();
                    calendar.setTime(startday);
                    calendar.add(Calendar.DATE, 1);
                    startday = calendar.getTime();
                    startDay = new SimpleDateFormat("yyyy-MM-dd").format(startday);
                    if (ObjectUtils.isEmpty(map.get(startDay))) {
                        statisticsVo1.setTime(startDay);
                        statisticsVo1.setCount(0L);
                    } else {
                        statisticsVo1.setTime(startDay);
                        statisticsVo1.setCount(map.get(startDay));
                    }
                    statisticsVo.add(statisticsVo1);
                }
            }
            baseRespose.setReturnString(statisticsVo);
        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("活跃用户统计 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("活跃用户统计 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 阅读时间段统计 staReadTime
     **/
    public BaseRespose staReadTime(BaseRequest baseRequest) {
        log.info("阅读时间段统计,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        String[] timeArray = Constants.TIME_ARRAY;
        try {
            PageVo pageVo = new PageVo();
            if (!ObjectUtils.isEmpty(baseRequest.getRequestString())) {
                try {
                    pageVo = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), PageVo.class);
                } catch (Exception e) {
                    throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL, JSON.toJSONString(baseRequest));
                }
            }
            List<StatisticsVo> statisticsVoList = new ArrayList<>();
            StatisticsVo statisticsVo = new StatisticsVo();
            for (String time : timeArray
                    ) {
                statisticsVo = new StatisticsVo();
                statisticsVo.setTime(time);
                pageVo.setHour(time);

                Long count = statisticsMapper.staReadTime(pageVo);
                statisticsVo.setCount(count);
                statisticsVoList.add(statisticsVo);
            }
            baseRespose.setReturnString(statisticsVoList);


        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("阅读时间段统计 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("阅读时间段统计 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 功能点击次数统计 staFunctionClick
     **/

    public BaseRespose staFunctionClick(BaseRequest baseRequest) {
        log.info("功能点击次数统计,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        try {
            PageVo pageVo = new PageVo();
            if (!ObjectUtils.isEmpty(baseRequest.getRequestString())) {
                try {
                    pageVo = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), PageVo.class);
                } catch (Exception e) {
                    throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL, JSON.toJSONString(baseRequest));
                }
            }
            List<StatisticsVo> statisticsVoList = statisticsMapper.staFunctionClick(pageVo);
            baseRespose.setReturnString(statisticsVoList);
        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("功能点击次数统计 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("功能点击次数统计 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 小说类型累计阅读人数 staBookTypeReadSum
     **/
    public BaseRespose staBookTypeReadSum(BaseRequest baseRequest) {
        log.info("小说类型累计阅读人数,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        try {
            List<StatisticsVo> statisticsVoList = statisticsMapper.staBookTypeReadSum();

            baseRespose.setReturnString(statisticsVoList);
        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("小说类型累计阅读人数 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("小说类型累计阅读人数 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 不活跃用户统计 staNotActiveUser
     **/
    public BaseRespose staNotActiveUser(BaseRequest baseRequest) {
        log.info("不活跃用户统计,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.add(Calendar.DATE, -Constants.LOGIN_DAY);
            UserinformationExample example = new UserinformationExample();
            example.createCriteria().andLastlogintimeLessThan(calendar.getTime());
            int notActiveCount = userinformationMapper.countByExample(example);
            int count = userinformationMapper.countByExample(null);
            StatisticsVo statisticsVo = new StatisticsVo();
            statisticsVo.setCount(Long.valueOf(count));
            statisticsVo.setCountTwo(Long.valueOf(notActiveCount));
            baseRespose.setReturnString(statisticsVo);

        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("不活跃用户统计 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("不活跃用户统计 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 热门小说统计 selectHotBook
     **/
    public BaseRespose selectHotBook(BaseRequest baseRequest) {
        log.info("热门小说统计,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        try {
            BookPageVo bookPageVo = null;
            try {
                bookPageVo = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), BookPageVo.class);
            } catch (Exception e) {
                throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL);
            }
            PageHelper.startPage(bookPageVo.getPagenum(), bookPageVo.getPagesize());
            List<Bookinformation> bookinformationList = bookinformationMapper.selectBookListByHot(bookPageVo);
            PageInfo<Bookinformation> pageInfo = new PageInfo<>(bookinformationList);
            PageResposeVo pageResposeVo = new PageResposeVo(pageInfo.getTotal(), pageInfo.getList());
            baseRespose.setReturnString(pageResposeVo);
        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("热门小说统计 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("热门小说统计 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 根据收入查询喜欢类型 staBookTypeByIncome
     **/
    public BaseRespose staBookTypeByIncome(BaseRequest baseRequest) {
        log.info("根据收入查询喜欢类型,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        try {
            List<StatisticsVo> statisticsVoList = new ArrayList<>();
            StatisticsVo statisticsVo = new StatisticsVo();
            String[] incomeArray = Constants.INCOME_ARRAY;
            for (String income : incomeArray
                    ) {
                statisticsVo = statisticsMapper.staBookTypeByIncome(income);
                if (ObjectUtils.isEmpty(statisticsVo)) {
                    statisticsVo = new StatisticsVo();
                    statisticsVo.setName(" ");
                }
                statisticsVo.setTypename(income);
                statisticsVoList.add(statisticsVo);
            }
            statisticsVo = statisticsMapper.staBookTypeByIncomeForNull();
            if (ObjectUtils.isEmpty(statisticsVo)) {
                statisticsVo = new StatisticsVo();
                statisticsVo.setName(" ");
            }
            statisticsVo.setTypename("未选择收入");
            statisticsVoList.add(statisticsVo);
            baseRespose.setReturnString(statisticsVoList);

        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("根据收入查询喜欢类型 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("根据收入查询喜欢类型 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 用户男女比例统计 staUserSex
     **/
    public BaseRespose staUserSex(BaseRequest baseRequest) {
        log.info("用户男女比例统计,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        try {
            List<StatisticsVo> statisticsVoList = statisticsMapper.staUserSex();
            baseRespose.setReturnString(statisticsVoList);
        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("用户男女比例统计 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("用户男女比例统计 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 用户地区分布 staUserRegion
     **/
    public BaseRespose staUserRegion(BaseRequest baseRequest) {
        log.info("用户地区分布,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        try {
            List<StatisticsVo> statisticsVoList = statisticsMapper.staUserRegion();
            baseRespose.setReturnString(statisticsVoList);
        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("用户地区分布 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("用户地区分布 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 用户年龄分布 staUserAge
     **/
    public BaseRespose staUserAge(BaseRequest baseRequest) {
        log.info("用户年龄分布,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        List<StatisticsVo> statisticsVoList = new ArrayList<>();

        try {
            String[] strings = Constants.AGE_ARRAY;
            for (String s : strings
                    ) {
                StatisticsVo statisticsVo = new StatisticsVo();
                String[] strings1 = s.split("-");
                IntegerVo integerVo = new IntegerVo();
                integerVo.setMin(Integer.parseInt(strings1[0]));
                if (strings1.length == 2) {
                    integerVo.setMax(Integer.parseInt(strings1[1]));
                    statisticsVo.setName(s);
                } else {
                    statisticsVo.setName(s + "+");
                }
                Long count = statisticsMapper.staUserAge(integerVo);
                statisticsVo.setCount(count);
                statisticsVoList.add(statisticsVo);
            }
            StatisticsVo statisticsVo = new StatisticsVo();
            Long count = statisticsMapper.staUserAgeForNull();
            statisticsVo.setName("未选择");
            statisticsVo.setCount(count);
            statisticsVoList.add(statisticsVo);
            baseRespose.setReturnString(statisticsVoList);
        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("用户年龄分布 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("用户年龄分布 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 用户书籍类型分布 staUserBookBype
     **/
    public BaseRespose staUserBookBype(BaseRequest baseRequest) {
        log.info("用户书籍类型分布,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        try {
            List<StatisticsVo> statisticsVoList = statisticsMapper.staUserBookBype();
            baseRespose.setReturnString(statisticsVoList);
        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("用户书籍类型分布 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("用户书籍类型分布 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 用户阅读时长统计 staUserReadTime
     **/
    public BaseRespose staUserReadTime(BaseRequest baseRequest) {
        log.info("用户阅读时长统计,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        try {
            List<StatisticsVo> statisticsVoList = new ArrayList<>();
            String[] strings = Constants.READTIME_ARRAY;
            for (String s : strings
                    ) {
                StatisticsVo statisticsVo = new StatisticsVo();
                String[] strings1 = s.split("-");
                IntegerVo integerVo = new IntegerVo();
                integerVo.setMin(Integer.parseInt(strings1[0]));
                if (strings1.length == 2) {
                    integerVo.setMax(Integer.parseInt(strings1[1]));
                    statisticsVo.setName(s);
                } else {
                    statisticsVo.setName(s + "+");
                }
                Long count = statisticsMapper.staUserReadTime(integerVo);
                statisticsVo.setCount(count);
                statisticsVoList.add(statisticsVo);
            }
            baseRespose.setReturnString(statisticsVoList);
        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("用户阅读时长统计 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("用户阅读时长统计 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 用户收藏排行 staUserCollection
     **/
    public BaseRespose staUserCollection(BaseRequest baseRequest) {
        log.info("用户收藏排行,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        try {
            CollectionVo collectionVo = new CollectionVo();
            if (!ObjectUtils.isEmpty(baseRequest.getRequestString())) {
                try {
                    collectionVo = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), CollectionVo.class);
                } catch (Exception e) {
                    throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL, JSON.toJSONString(baseRequest));
                }
            }
            if (collectionVo.getSelectType() == 1) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.add(Calendar.DATE, -7);
                collectionVo.setSelecttime(calendar.getTime());
            }
            if (collectionVo.getSelectType() == 2) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.add(Calendar.MONTH, -1);
                collectionVo.setSelecttime(calendar.getTime());
            }
            if (collectionVo.getSelectType() == 3) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.add(Calendar.YEAR, -1);
                collectionVo.setSelecttime(calendar.getTime());
            }
            List<BookResultVo> bookResultVoList = statisticsMapper.staUserCollection(collectionVo);

            baseRespose.setReturnString(bookResultVoList);
        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("用户收藏排行 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("用户收藏排行 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 某本小说的男女比例 staUserSexByBook
     **/
    public BaseRespose staUserSexByBook(BaseRequest baseRequest) {
        log.info("某本小说的男女比例,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        try {
            BookPageVo bookPageVo = new BookPageVo();
            try {
                bookPageVo = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), BookPageVo.class);
            } catch (Exception e) {
                throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL, JSON.toJSONString(baseRequest));
            }
            if (ObjectUtils.isEmpty(bookPageVo.getBookid())) {
                throw new ServiceException(203020, "请选择书籍", JSON.toJSONString(baseRequest));
            }
            List<StatisticsVo> statisticsVoList = statisticsMapper.staUserSexByBook(bookPageVo.getBookid());
            if (statisticsVoList.size() == 0) {
                StatisticsVo statisticsVo = new StatisticsVo();
                statisticsVo.setName("男");
                statisticsVo.setCount(0L);
                statisticsVoList.add(statisticsVo);
                statisticsVo = new StatisticsVo();
                statisticsVo.setName("女");
                statisticsVo.setCount(0L);
                statisticsVoList.add(statisticsVo);
            } else if (statisticsVoList.size() == 1) {
                StatisticsVo statisticsVo = statisticsVoList.get(0);
                if (statisticsVo.getName().equals("男")) {
                    StatisticsVo statisticsVo1 = new StatisticsVo();
                    statisticsVo1.setName("女");
                    statisticsVo1.setCount(0L);
                    statisticsVoList.add(statisticsVo1);
                } else {
                    StatisticsVo statisticsVo1 = new StatisticsVo();
                    statisticsVo1.setName("男");
                    statisticsVo1.setCount(0L);
                    statisticsVoList.add(statisticsVo1);
                }
            }
            baseRespose.setReturnString(statisticsVoList);

        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("某本小说的男女比例 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("某本小说的男女比例 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 某本小说的地区分布 staUserRegionByBook
     **/
    public BaseRespose staUserRegionByBook(BaseRequest baseRequest) {
        log.info("某本小说的地区分布,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        try {
            BookPageVo bookPageVo = new BookPageVo();
            try {
                bookPageVo = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), BookPageVo.class);
            } catch (Exception e) {
                throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL, JSON.toJSONString(baseRequest));
            }
            if (ObjectUtils.isEmpty(bookPageVo.getBookid())) {
                throw new ServiceException(203020, "请选择书籍", JSON.toJSONString(baseRequest));
            }
            List<StatisticsVo> statisticsVoList = statisticsMapper.staUserRegionByBook(bookPageVo.getBookid());
            baseRespose.setReturnString(statisticsVoList);

        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("某本小说的地区分布 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("某本小说的地区分布 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 某本小说的收入分布 staUserIncomeByBook
     **/
    public BaseRespose staUserIncomeByBook(BaseRequest baseRequest) {
        log.info("某本小说的收入分布,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        try {
            BookPageVo bookPageVo = new BookPageVo();
            try {
                bookPageVo = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), BookPageVo.class);
            } catch (Exception e) {
                throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL, JSON.toJSONString(baseRequest));
            }
            if (ObjectUtils.isEmpty(bookPageVo.getBookid())) {
                throw new ServiceException(203020, "请选择书籍", JSON.toJSONString(baseRequest));
            }
            List<StatisticsVo> statisticsVoList = new ArrayList<>();
            String[] incomeArray=Constants.INCOME_ARRAY;
            for (String income:incomeArray
                 ) {
                bookPageVo.setIncome(income);
                Long count=statisticsMapper.staUserIncomeByBook(bookPageVo);
                StatisticsVo statisticsVo=new StatisticsVo();
                statisticsVo.setCount(count);
                statisticsVo.setName(income);
                statisticsVoList.add(statisticsVo);
            }
            Long count=statisticsMapper.staUserIncomeByBookForNull(bookPageVo.getBookid());
            StatisticsVo statisticsVo=new StatisticsVo();
            statisticsVo.setCount(count);
            statisticsVo.setName("未选择");
            statisticsVoList.add(statisticsVo);
            baseRespose.setReturnString(statisticsVoList);
        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("某本小说的收入分布 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("某本小说的收入分布 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }
    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 弹幕列表查询 selectChatrecord
     **/
    public BaseRespose selectChatrecord(BaseRequest baseRequest){
        log.info("弹幕列表查询,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        try {

        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("弹幕列表查询 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("弹幕列表查询 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

}
